package org.csu.logistics.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.logistics.common.CommonResponse;
import org.csu.logistics.entity.TransCenter;
import org.csu.logistics.entity.Warehouse;
import org.csu.logistics.persistence.TransCenterMapper;
import org.csu.logistics.persistence.WarehouseMapper;
import org.csu.logistics.vo.WarehouseOV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("WarehouseService")
public class WarehouseServiceImp implements WarehouseService{

    @Autowired
    WarehouseMapper warehouseMapper;
    @Autowired
    TransCenterService transCenterService;

    @Override
    public void insert_wareHouse(Warehouse warehouse) {
        warehouseMapper.insert(warehouse);
    }

    @Override
    public void delete_wareHouseByOrderNum(int orderNum) {
        warehouseMapper.deleteById(orderNum);
    }

    @Override
    public void delete_wareHouseByOrderNumAndWareHouseId(int orderNum, int warehouseId) {
        QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ordernum",orderNum).eq("warehouseid",warehouseId);
        warehouseMapper.delete(queryWrapper);
    }

    @Override
    public int update_wareHouseByOrderNum(Warehouse warehouse,String locale) {
        TransCenter transCenter = transCenterService.select_transCenterByLocale(locale);
        int warehouseId = transCenter.getWarehouseId();
        warehouse.setWarehouseId(warehouseId);
        QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ordernum",warehouse.getOrderNum()).eq("warehouseid",warehouseId);
        return warehouseMapper.update(warehouse,queryWrapper);
    }

    @Override
    public List<Warehouse> select_wareHouseByOrderNum(int orderNum) {
        QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ordernum",orderNum);
        List<Warehouse> warehouseList = warehouseMapper.selectList(queryWrapper);
        if(warehouseList==null){
            return null;
        }
        return warehouseList;
    }

    @Override
    public CommonResponse<Warehouse> select_wareHouseByOrderNumAndWareHouseId(int orderNum, int warehouseId) {
        QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ordernum",orderNum).eq("warehouseid",warehouseId);
        Warehouse warehouseList = warehouseMapper.selectOne(queryWrapper);
        if(warehouseList==null){
            return CommonResponse.createForError("没有该订单在"+ warehouseId +"仓库信息");
        }
        return CommonResponse.createForSuccess(warehouseList);
    }

    @Override
    public CommonResponse<List<WarehouseOV>> select_warehouse(){
        List<Warehouse> warehouseList = warehouseMapper.selectList(null);
        if(warehouseList==null){
            return CommonResponse.createForError("没有运输中订单");
        }
        List<WarehouseOV> warehouseOVList = new ArrayList<>();
        for(int i=0;i<warehouseList.size();i++){
            Warehouse warehouse = warehouseList.get(i);
            TransCenter transCenter = transCenterService.select_transCenterByWareHouseId(warehouse.getWarehouseId());
            WarehouseOV warehouseOV = new WarehouseOV();
            warehouseOV.setWarehouseId(warehouse.getWarehouseId());
            warehouseOV.setLocale(transCenter.getLocale());
            warehouseOV.setOrderNum(warehouse.getOrderNum());
            warehouseOV.setStatus(warehouse.getStatus());
            warehouseOVList.add(warehouseOV);
        }
        return CommonResponse.createForSuccess(warehouseOVList);
    }

    @Override
    public CommonResponse<int[]> select_orderNum(){
        List<Warehouse> warehouseList = warehouseMapper.selectList(null);
        if(warehouseList==null){
            return CommonResponse.createForError("没有运输中订单");
        }
        int[] orderNums = new int[1000];
        for(int i=0;i<1000;i++){
            orderNums[i] = 0;
        }
        int I = 0;
        for(int i=0;i<warehouseList.size();i++){
            if(i==0){
                orderNums[I] = warehouseList.get(i).getOrderNum();
                I++;
            }
            else{
                int flag=0;
                for(int j=0;j<1000;j++){
                    if(orderNums[j] == warehouseList.get(i).getOrderNum()){
                        flag=1;
                        break;
                    }
                }
                if(flag==0){
                    orderNums[I]=warehouseList.get(i).getOrderNum();
                    I++;
                }
            }
        }

        int[] orderNum = new int[I];
        for(int i=0;i<I;i++){
            orderNum[i] = orderNums[i];
        }
        return CommonResponse.createForSuccess(orderNum);
    }

    @Override
    public CommonResponse<List<WarehouseOV>> select_warehouseOVByOrderNum(int orderNum) {
        QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ordernum",orderNum);
        List<Warehouse> warehouseList = warehouseMapper.selectList(queryWrapper);
        if(warehouseList==null){
            return CommonResponse.createForError("该用户没有运输中订单");
        }
        List<WarehouseOV> warehouseOVList = new ArrayList<>();
        for(int i=0;i<warehouseList.size();i++){
            Warehouse warehouse = warehouseList.get(i);
            TransCenter transCenter = transCenterService.select_transCenterByWareHouseId(warehouse.getWarehouseId());
            WarehouseOV warehouseOV = new WarehouseOV();
            warehouseOV.setWarehouseId(warehouse.getWarehouseId());
            warehouseOV.setLocale(transCenter.getLocale());
            warehouseOV.setOrderNum(warehouse.getOrderNum());
            warehouseOV.setStatus(warehouse.getStatus());
            if(!warehouseOV.getStatus().equals("未到达")){
                warehouseOV.setColor("#0bbd87");
            }
            warehouseOVList.add(warehouseOV);
        }
        return CommonResponse.createForSuccess(warehouseOVList);
    }

    @Override
    public CommonResponse<List<WarehouseOV>> select_warehouseOVByLocale(String locale){
        List<Warehouse> warehouseList = warehouseMapper.selectList(null);
        if(warehouseList==null){
            return CommonResponse.createForError("没有运输中订单");
        }
        List<WarehouseOV> warehouseOVList = new ArrayList<>();
        for(int i=0;i<warehouseList.size();i++){
            Warehouse warehouse = warehouseList.get(i);
            TransCenter transCenter = transCenterService.select_transCenterByWareHouseId(warehouse.getWarehouseId());
            WarehouseOV warehouseOV = new WarehouseOV();
            warehouseOV.setWarehouseId(warehouse.getWarehouseId());
            warehouseOV.setLocale(transCenter.getLocale());
            warehouseOV.setOrderNum(warehouse.getOrderNum());
            warehouseOV.setStatus(warehouse.getStatus());
            if((warehouseOV.getLocale()).equals(locale)){
                warehouseOVList.add(warehouseOV);
            }
        }
        return CommonResponse.createForSuccess(warehouseOVList);
    }
}
